package com.haiqiu.tools.common.utils;

import cn.hutool.core.util.RandomUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author HaiQiu
 * @since 2023/3/3
 */
public class FileToolsUtils {

    static String CARVE = "/";

    static String CAS = ".";

    static String HORIZONTAL = "-";

    /**
     * 创建oss文件上传随机文件名
     *
     * @param destPath      目标目录
     * @param multipartFile 文件
     * @return 返回随机目标文件地址和名
     */
    public static String createOssFileName(String destPath, MultipartFile multipartFile) {
        if (destPath.startsWith(CARVE)) {
            destPath = destPath.substring(1);
        }
        if (!destPath.endsWith(CARVE)) {
            destPath = destPath + CARVE;
        }
        String folder = destPath.substring(0, destPath.lastIndexOf(CARVE) + 1);
        String filename = destPath.substring(destPath.lastIndexOf(CARVE) + 1);
        String originalFilename = multipartFile.getOriginalFilename();
        String prefix = originalFilename.substring(0, originalFilename.lastIndexOf(CAS));
        //截取成注意有点 .mp3
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(CAS));

        if (!StringUtils.hasText(filename)) {
            long millis = System.currentTimeMillis();
            filename = millis + RandomUtil.randomNumbers(10) + suffix;
            return destPath + filename;
        }
        return destPath + filename;
    }

}
